home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / yuv2rgbPhilips.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-22  |  10.5 KB  |  288 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: yuv2rgbPhilips.c,v 1.1.1.1 2002/05/22 04:57:22 dirkx Exp $
  19. */
  20.  
  21. /*
  22.  
  23.  This file contains just a raw function body and is intended to be #included from other files, such as yuv2rgb.c
  24.  
  25.  The function prototype has to be:
  26.  
  27.  void <whatever>(int width, int height, unsigned char *src, unsigned char *dst, long srcRowExtra, long dstRowExtra);
  28.  
  29.  */
  30.  
  31. unsigned char* s1;                //Source buffer run: first line
  32.     unsigned char* s2;                //Source buffer run: second line
  33.     unsigned char* d1;                //Destination buffer run: first line
  34.     unsigned char* d2;                //Destination buffer run: first line
  35.     long x,y;                    //loop counters
  36.     long y11,y12,y13,y14,y21,y22,y23,y24;    //The y components in yuv
  37.     long u1,u1g,u1b,u2,u2g,u2b;            //The raw u components and the u differences
  38.     long v1,v1r,v1g,v2,v2r,v2g;            //The raw v components and the v differences
  39.     long r11,g11,b11,r12,g12,b12,r13,g13,b13,r14,g14,b14;    //Destination rgb: first line
  40.     long r21,g21,b21,r22,g22,b22,r23,g23,b23,r24,g24,b24;    //Destination rgb: second line
  41.     long dstRowBytes;                //EXCLUDING the extra part, just the raw data length per row
  42.     unsigned long ul1,ul2,ul3,ul4,ul5,ul6,ul7,ul8;     //Temp vars to access memory
  43.     unsigned short us1,us2;            //Temp vars to access memory
  44. #ifdef YUV2RGB_ALPHA
  45.     short bpp=4;
  46. #else
  47.     short bpp=3;
  48. #pragma unused(ul4)
  49. #pragma unused(ul8)
  50. #endif
  51.     width/=4;                    //We work in 4 x 2 blocks
  52.     height/=2;
  53.     dstRowBytes=4*bpp*width;
  54.     s1=src;
  55.     s2=src+width*6+srcRowExtra;
  56.     srcRowExtra=2*srcRowExtra+6*width;        //skip line since we're working two lines at once
  57.  
  58.     d1=dst;
  59.     d2=dst+width*4*bpp+dstRowExtra;
  60. #ifndef YUV2RGB_FLIP
  61.     dstRowExtra=2*dstRowExtra+dstRowBytes;    //Extend to skip one line since we're working two lines at once
  62. #else
  63.     dstRowExtra=2*dstRowExtra+3*dstRowBytes;    //From the start of a line to the end of the second next
  64.     d1+=dstRowBytes;
  65.     d2+=dstRowBytes;
  66. #endif
  67.     for (y=height;y;y--) {
  68.         for (x=width;x;x--) {
  69. //Read from source buffer
  70.             ul1=*((unsigned  long*)(s1)); s1+=4;    //Read y in line 1
  71.             us1=*((unsigned short*)(s1)); s1+=2;    //Read all u
  72.             ul2=*((unsigned  long*)(s2)); s2+=4;    //Read y in line 2
  73.             us2=*((unsigned short*)(s2)); s2+=2;    //Read all v
  74. //Extract yuv pixel data
  75.             y11=(ul1&0xff000000)>>16;
  76.             y12=(ul1&0x00ff0000)>>8;
  77.             y13=(ul1&0x0000ff00);
  78.             y14=(ul1&0x000000ff)<<8;
  79.             y21=(ul2&0xff000000)>>16;
  80.             y22=(ul2&0x00ff0000)>>8;
  81.             y23=(ul2&0x0000ff00);
  82.             y24=(ul2&0x000000ff)<<8;
  83.             u1 =((long)((us1&0xff00)>>8))-128;
  84.             u2 =((long)((us1&0x00ff)   ))-128;
  85.             v1 =((long)((us2&0xff00)>>8))-128;
  86.             v2 =((long)((us2&0x00ff)   ))-128;
  87. //convert yuv to rgb: calculate difference coefficients
  88.             u1g=u1*88;
  89.             u1b=u1*454;
  90.             v1r=v1*359;
  91.             v1g=v1*183;
  92.             u2g=u2*88;
  93.             u2b=u2*454;
  94.             v2r=v2*359;
  95.             v2g=v2*183;
  96. //convert yuv to rgb: assemble rgb
  97.             r11=(y11+v1r)/256;
  98.             g11=(y11-u1g-v1g)/256;
  99.             b11=(y11+u1b)/256;
  100.             r12=(y12+v1r)/256;
  101.             g12=(y12-u1g-v1g)/256;
  102.             b12=(y12+u1b)/256;
  103.             r13=(y13+v2r)/256;
  104.             g13=(y13-u2g-v2g)/256;
  105.             b13=(y13+u2b)/256;
  106.             r14=(y14+v2r)/256;
  107.             g14=(y14-u2g-v2g)/256;
  108.             b14=(y14+u2b)/256;
  109.             r21=(y21+v1r)/256;
  110.             g21=(y21-u1g-v1g)/256;
  111.             b21=(y21+u1b)/256;
  112.             r22=(y22+v1r)/256;
  113.             g22=(y22-u1g-v1g)/256;
  114.             b22=(y22+u1b)/256;
  115.             r23=(y23+v2r)/256;
  116.             g23=(y23-u2g-v2g)/256;
  117.             b23=(y23+u2b)/256;
  118.             r24=(y24+v2r)/256;
  119.             g24=(y24-u2g-v2g)/256;
  120.             b24=(y24+u2b)/256;
  121. //convert yuv to rgb: check value bounds
  122.             r11=CLAMP(r11,0,255);
  123.             g11=CLAMP(g11,0,255);
  124.             b11=CLAMP(b11,0,255);
  125.             r12=CLAMP(r12,0,255);
  126.             g12=CLAMP(g12,0,255);
  127.             b12=CLAMP(b12,0,255);
  128.             r13=CLAMP(r13,0,255);
  129.             g13=CLAMP(g13,0,255);
  130.             b13=CLAMP(b13,0,255);
  131.             r14=CLAMP(r14,0,255);
  132.             g14=CLAMP(g14,0,255);
  133.             b14=CLAMP(b14,0,255);
  134.             r21=CLAMP(r21,0,255);
  135.             g21=CLAMP(g21,0,255);
  136.             b21=CLAMP(b21,0,255);
  137.             r22=CLAMP(r22,0,255);
  138.             g22=CLAMP(g22,0,255);
  139.             b22=CLAMP(b22,0,255);
  140.             r23=CLAMP(r23,0,255);
  141.             g23=CLAMP(g23,0,255);
  142.             b23=CLAMP(b23,0,255);
  143.             r24=CLAMP(r24,0,255);
  144.             g24=CLAMP(g24,0,255);
  145.             b24=CLAMP(b24,0,255);
  146. //Assemble longs from rgb data
  147. #ifndef YUV2RGB_ALPHA
  148. #ifdef YUV2RGB_FLIP
  149. //3bpp, flipped assembly
  150.             ul3=(((unsigned long)r14)<<24)
  151.                 |(((unsigned long)g14)<<16)
  152.                 |(((unsigned long)b14)<<8)
  153.                 |(((unsigned long)r13));
  154.             ul2=(((unsigned long)g13)<<24)
  155.                 |(((unsigned long)b13)<<16)
  156.                 |(((unsigned long)r12)<<8)
  157.                 |(((unsigned long)g12));
  158.             ul1=(((unsigned long)b12)<<24)
  159.                 |(((unsigned long)r11)<<16)
  160.                 |(((unsigned long)g11)<<8)
  161.                 |(((unsigned long)b11));
  162.             ul7=(((unsigned long)r24)<<24)
  163.                 |(((unsigned long)g24)<<16)
  164.                 |(((unsigned long)b24)<<8)
  165.                 |(((unsigned long)r23));
  166.             ul6=(((unsigned long)g23)<<24)
  167.                 |(((unsigned long)b23)<<16)
  168.                 |(((unsigned long)r22)<<8)
  169.                 |(((unsigned long)g22));
  170.             ul5=(((unsigned long)b22)<<24)
  171.                 |(((unsigned long)r21)<<16)
  172.                 |(((unsigned long)g21)<<8)
  173.                 |(((unsigned long)b21));
  174. #else
  175. //3bpp, unflipped assembly
  176.             ul1=(((unsigned long)r11)<<24)
  177.                 |(((unsigned long)g11)<<16)
  178.                 |(((unsigned long)b11)<<8)
  179.                 |(((unsigned long)r12));
  180.             ul2=(((unsigned long)g12)<<24)
  181.                 |(((unsigned long)b12)<<16)
  182.                 |(((unsigned long)r13)<<8)
  183.                 |(((unsigned long)g13));
  184.             ul3=(((unsigned long)b13)<<24)
  185.                 |(((unsigned long)r14)<<16)
  186.                 |(((unsigned long)g14)<<8)
  187.                 |(((unsigned long)b14));
  188.             ul5=(((unsigned long)r21)<<24)
  189.                 |(((unsigned long)g21)<<16)
  190.                 |(((unsigned long)b21)<<8)
  191.                 |(((unsigned long)r22));
  192.             ul6=(((unsigned long)g22)<<24)
  193.                 |(((unsigned long)b22)<<16)
  194.                 |(((unsigned long)r23)<<8)
  195.                 |(((unsigned long)g23));
  196.             ul7=(((unsigned long)b23)<<24)
  197.                 |(((unsigned long)r24)<<16)
  198.                 |(((unsigned long)g24)<<8)
  199.                 |(((unsigned long)b24));
  200. #endif
  201. #else
  202. //4bpp assembly - no matter if flipped or unflipped
  203.             ul1=0xff000000
  204.                 |(((unsigned long)r11)<<16)
  205.                 |(((unsigned long)g11)<<8)
  206.                 |(((unsigned long)b11));
  207.             ul2=0xff000000
  208.                 |(((unsigned long)r12)<<16)
  209.                 |(((unsigned long)g12)<<8)
  210.                 |(((unsigned long)b12));
  211.             ul3=0xff000000
  212.                 |(((unsigned long)r13)<<16)
  213.                 |(((unsigned long)g13)<<8)
  214.                 |(((unsigned long)b13));
  215.             ul4=0xff000000
  216.                 |(((unsigned long)r14)<<16)
  217.                 |(((unsigned long)g14)<<8)
  218.                 |(((unsigned long)b14));
  219.             ul5=0xff000000
  220.                 |(((unsigned long)r21)<<16)
  221.                 |(((unsigned long)g21)<<8)
  222.                 |(((unsigned long)b21));
  223.             ul6=0xff000000
  224.                 |(((unsigned long)r22)<<16)
  225.                 |(((unsigned long)g22)<<8)
  226.                 |(((unsigned long)b22));
  227.             ul7=0xff000000
  228.                 |(((unsigned long)r23)<<16)
  229.                 |(((unsigned long)g23)<<8)
  230.                 |(((unsigned long)b23));
  231.             ul8=0xff000000
  232.                 |(((unsigned long)r24)<<16)
  233.                 |(((unsigned long)g24)<<8)
  234.                 |(((unsigned long)b24));
  235. #endif
  236. //Output to destination buffer
  237. #ifdef YUV2RGB_FLIP
  238. #ifdef YUV2RGB_ALPHA
  239.             d1-=16;
  240.             *((unsigned long*)(d1+12))=ul1;
  241.             *((unsigned long*)(d1+ 8))=ul2;
  242.             *((unsigned long*)(d1+ 4))=ul3;
  243.             *((unsigned long*)(d1   ))=ul4;
  244.             d2-=16;
  245.             *((unsigned long*)(d2+12))=ul5;
  246.             *((unsigned long*)(d2+ 8))=ul6;
  247.             *((unsigned long*)(d2+ 4))=ul7;
  248.             *((unsigned long*)(d2   ))=ul8;
  249. #else    //YUV2RGB_ALPHA
  250.             d1-=12;
  251.             *((unsigned long*)(d1+ 8))=ul1;
  252.             *((unsigned long*)(d1+ 4))=ul2;
  253.             *((unsigned long*)(d1   ))=ul3;
  254.             d2-=12;
  255.             *((unsigned long*)(d2+ 8))=ul5;
  256.             *((unsigned long*)(d2+ 4))=ul6;
  257.             *((unsigned long*)(d2   ))=ul7;
  258. #endif    //YUV2RGB_ALPHA
  259. #else    //YUV2RGB_FLIP
  260. #ifdef YUV2RGB_ALPHA
  261.             *((unsigned long*)(d1))=ul1;
  262.             *((unsigned long*)(d1+4))=ul2;
  263.             *((unsigned long*)(d1+8))=ul3;
  264.             *((unsigned long*)(d1+12))=ul4;
  265.             d1+=16;
  266.             *((unsigned long*)(d2))=ul5;
  267.             *((unsigned long*)(d2+4))=ul6;
  268.             *((unsigned long*)(d2+8))=ul7;
  269.             *((unsigned long*)(d2+12))=ul8;
  270.             d2+=16;
  271. #else    //YUV2RGB_ALPHA
  272.             *((unsigned long*)(d1))=ul1;
  273.             *((unsigned long*)(d1+4))=ul2;
  274.             *((unsigned long*)(d1+8))=ul3;
  275.             d1+=12;
  276.             *((unsigned long*)(d2))=ul5;
  277.             *((unsigned long*)(d2+4))=ul6;
  278.             *((unsigned long*)(d2+8))=ul7;
  279.             d2+=12;
  280. #endif    //YUV2RGB_ALPHA
  281. #endif    //YUV2RGB_FLIP
  282.         }
  283.         s1+=srcRowExtra;
  284.         s2+=srcRowExtra;
  285.         d1+=dstRowExtra;
  286.         d2+=dstRowExtra;
  287.     }
  288. //End of included, preprocessor-customized code